<?php


use Biz\model\brandActivity\BrandJoinRecord;
use Biz\model\brandActivity\BrandShowActivity;

/**
 * @deprecated
 * Description: 品牌投票活动
 * Created by PhpStorm.
 * User: congyukun
 * Date: 2021/3/12
 * Time: 17:38
 */
class BrandVoteAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;
    
    public function main()
    {
        $brand_id = $this->getRequest()->getPost('brand_id');
        $brand_name = $this->getRequest()->getPost('brand_name');
        
        
        if (empty($brand_id) || empty($brand_name)) {
            throw new \Exception('非法请求');
        }
        
        $uid = $this->uid;
        $key = $uid . '_brand_act_vote';
        //5秒禁止提交
        $lock = \Biz\cache\brand\BrandShowActivity::isLock($key, 5);
        if (!$lock) {
    
            //获取参加活动品牌的id
            $where['status'] = BrandShowActivity::STATUS_NORMAL;
            $brand_info = BrandShowActivity::getAllNormalBrandIds(['brand_id', 'link_type', 'coupon_link']);
            $brand_info = array_column($brand_info,null,'brand_id');
            $brand_ids = array_keys($brand_info);
            
            if (!$brand_ids) {
                throw new \Exception('未有品牌参加活动');
            }
    
            if (!in_array($brand_id, $brand_ids)) {
                throw new \Exception('非法请求');
            }
            
            
            $data['uid'] = $uid;
            $data['brand_id'] = $brand_id;
            $data['brand_name'] = $brand_name;
            $data['fans'] = \Doraemon\cache\UserCounter::getOne($uid)['fans'];
            $data['create_time'] = date("Y-m-d H:i:s");
            
            
            $is_have = BrandJoinRecord::is_have(['uid' => $uid, 'brand_id' => $brand_id]);
            
            if ($is_have) {
                throw new \Exception('已经支持啦，不要再点了！');
            }
            $id = BrandJoinRecord::insertData($data);
            
            if (!$id) {
                throw new \Exception('出现异常，请重试！');
            }
            
            $result['link_type'] = $brand_info[$brand_id]['link_type']; //1：淘宝  2：京东
            $result['coupon_link'] = $brand_info[$brand_id]['coupon_link'] ?? '';
            $this->setSuccess($result);
        } else {
            throw new \Exception('你的操作太频繁');
        }
    }
    

}